package com.smart.manager.work.repository;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.crypto.digest.DigestUtil;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.work.entities.body.RBasicStaff;
import com.smart.manager.work.entities.enums.PigOrderStatus;
import com.smart.manager.work.entities.models.BasicStaff;
import com.smart.manager.work.entities.models.PigOrder;
import com.smart.manager.work.entities.models.query.QBasicStaff;
import com.smart.manager.work.entities.models.query.QPigOrder;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 管家管理
 *
 * @author leo
 * @date 2023/09/10
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class BasicStaffRepository {

    public Page<BasicStaff> pageList(RBasicStaff.SearchBody search) {
        QBasicStaff query = new QBasicStaff();
        Opt.ofBlankAble(search.getCode()).ifPresent(x -> query.code.contains(x));
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        Opt.ofBlankAble(search.getPhone()).ifPresent(x -> query.phone.eq(x));
        Opt.ofBlankAble(search.getName()).ifPresent(x -> query.name.contains(x));
        query.order().sort.asc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<BasicStaff>().load(query.findPagedList());
    }

    public void add(RBasicStaff.EditBody body) {
        QBasicStaff query = new QBasicStaff();
        BasicStaff record = query.code.eq(body.getCode()).setMaxRows(1).findOne();
        Assert.isNull(record, "创建失败:, 管家代码已存在");
        BasicStaff staff = BeanUtil.copyProperties(body, BasicStaff.class);
        staff.setPassword(DigestUtil.bcrypt(body.getPassword())).insert();
    }


    public void update(Long id, RBasicStaff.EditBody body) {
        QBasicStaff query = new QBasicStaff();
        BasicStaff record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();
    }

    public int delByIds(List<Long> ids) {
        List<BasicStaff> list = new QBasicStaff().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        for (BasicStaff item : list) {
            log.warn("判断数据是否能被删除");

            Optional<PigOrder> empty = new QPigOrder().staff.code.eq(item.getCode()).status.ne(PigOrderStatus.COMPLETED).setMaxRows(1).findOneOrEmpty();
           Assert.isFalse(empty.isEmpty(),"删除失败 "+item.getName()+" 还有工单进行中");

        }
        return DB.deleteAll(list);


    }

    /**
     * 获取数据字典下拉选项
     *
     * @return {@code List<SelectOption>}
     */
    public List<SelectOption> getOptions() {
        QBasicStaff place = new QBasicStaff();
        return place.findStream()
                .map(x -> new SelectOption().setLabel(x.getName()).setValue(x.getCode()).setDisabled(false))
                .collect(Collectors.toList());
    }

    /**
     * 获取数据字典下拉选项
     *
     * @return {@code List<SelectOption>}
     */
    public List<SelectOption> getIdOptions() {
        QBasicStaff place = new QBasicStaff();
        return place.findStream()
                .map(x -> new SelectOption().setLabel(x.getName()).setValue(String.valueOf(x.getId())).setDisabled(false))
                .collect(Collectors.toList());
    }


}






